﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Runtime.Remoting.Messaging;
using Red_Cross_Philippines;
using System.IO;

namespace Philippine_National_Red_Cross
{
    public partial class Search : Form
    {
        Blood_Bank_System BloodSystem = new Blood_Bank_System();
        DataTable tbl = new DataTable();
        

        public Search()
        {
            InitializeComponent();
            
            btnSearch.Click += new EventHandler(btnSearch_Click);
            dataGridView1.MouseDown += new MouseEventHandler(dataGridView1_MouseDown);
            this.Load += new EventHandler(Form3_Load);
            MenuEdit_Admin.Click += new EventHandler(MenuEdit_Admin_Click);
            lblBloodDonor.Click += new EventHandler(lblBloodDonor_Click);
            lblAdmin.Click += new EventHandler(lblAdmin_Click);
            MenuSearch.Click += new EventHandler(MenuSearch_Click);
            printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
            MenuPrint.Click += new EventHandler(MenuPrint_Click);
            txtSearch.TextChanged += new EventHandler(txtSearch_TextChanged);
            dataGridView1.DoubleClick += new EventHandler(dataGridView1_DoubleClick);
            
        }

        void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int curIndex = dataGridView1.SelectedRows[0].Index;
                byte[] imgdata = (byte[])BloodSystem.tbl.Rows[curIndex]["Image"];
                Form1 frm = new Form1(imgdata, curIndex);
                frm.ShowDialog();
            }
            catch (Exception) { }
                
               
        }
        delegate void SearchAutoSuggest();
        private void SearchNames()
        {
            if (txtSearch.InvokeRequired)
            {
                SearchAutoSuggest sas = new SearchAutoSuggest(SearchNames);
                txtSearch.Invoke(sas, new object[] { });
            }
            else
            {
                try
                {
                    txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
                    txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
                    BloodSystem.Connect();
                    string sql = "SELECT Name FROM tblblooddonor WHERE Name LIKE '%" + txtSearch.Text + "%'";
                    BloodSystem.Query(sql);
                    tbl = BloodSystem.tbl;


                    if (tbl.Rows.Count > 0)
                    {
                        for (int i = 0; i < tbl.Rows.Count; i++)
                        {
                            collection.Add(tbl.Rows[i].ItemArray[0].ToString());
                           
                        }
                    }
                    txtSearch.AutoCompleteCustomSource = collection;
                }
                catch (Exception) { }
                
                BloodSystem.Disconnect();
            }
        }
        void txtSearch_TextChanged(object sender, EventArgs e)
        {

            SearchNames();

        }

        void MenuPrint_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count != 0)
            {
                printDocument1.Print();
            }
            else
            {
                MessageBox.Show("No file to print", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
        }

      

        void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bm = new Bitmap(this.dataGridView1.Width, dataGridView1.Height);
            dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
            e.Graphics.DrawImage(bm, 0, 0);
        }

       
       
      

        void MenuSearch_Click(object sender, EventArgs e)
        {
            txtSearch.Focus();
        }

     

        
        void lblAdmin_Click(object sender, EventArgs e)
        {
            lblAdmin.ForeColor = Color.Red;
            lblBloodDonor.ForeColor = Color.Black;
        }

        void lblBloodDonor_Click(object sender, EventArgs e)
        {
            lblBloodDonor.ForeColor = Color.Red;
            lblAdmin.ForeColor = Color.Black;
        }
       
      
        void MenuEdit_Admin_Click(object sender, EventArgs e)
        {
           EditAdministrator();
        }

        void Form3_Load(object sender, EventArgs e)
        {
            BloodSystem.Connect();
            BloodSystem.Disconnect();
        }


  

        void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip2.Show(Cursor.Position);
            }
        }


     
      void btnSearch_Click(object sender, EventArgs e)
        {
            if (lblAdmin.ForeColor == Color.Red)
            {
                MenuEdit_Admin.Enabled = true;
                MenuEdit_Clients.Enabled = false;
                string sql = "SELECT Name,Email,Username,Password,Position FROM tblusers WHERE Name LIKE '%" + txtSearch.Text + "%'";
                BloodSystem.Query(sql);
                tbl = BloodSystem.tbl;
                dataGridView1.DataSource = tbl;
            }
            else if (lblBloodDonor.ForeColor == Color.Red)
            {
                MenuEdit_Admin.Enabled = false;
                MenuEdit_Clients.Enabled = true;
                string sql = "SELECT Name,BloodType,Age,Sex,Birthdate,Address,Contact,Image FROM tblblooddonor WHERE Name LIKE '%" + txtSearch.Text + "%'";
                BloodSystem.Query(sql);
                tbl = BloodSystem.tbl;
                dataGridView1.DataSource = tbl;
            }

        
        }

      public void EditAdministrator()
      {
          int curIndex = dataGridView1.SelectedRows[0].Index;
          string name = BloodSystem.tbl.Rows[curIndex][0].ToString();
          string emailaddress = BloodSystem.tbl.Rows[curIndex][1].ToString();
          string username = BloodSystem.tbl.Rows[curIndex][2].ToString();
          string password = BloodSystem.tbl.Rows[curIndex][3].ToString();
          string position = BloodSystem.tbl.Rows[curIndex][4].ToString();
          frmUpdate frm4 = new frmUpdate(name, emailaddress, username, password, curIndex);
          frm4.ShowDialog();
      }

       
    }
}
